import cloud from '@lafjs/cloud'
let data
let dbName = "admin-class-ToC"
const db = cloud.mongo.db
import { pinyin } from "pinyin-pro";


export default async function (ctx: FunctionContext) {
  data = ctx.body.data

  switch (ctx.body.type) {
    case "list":
      return list()
      break;
    case "add":
      return add()
    case "update"://更新
      return cloud.utils.updateData(dbName, data)
    case "del":
      return del()
      break;
    case "details":
      return details()
    case "studentsList"://班级学员
      return studentsList()
      break;
    case "studentImport"://学生导入
      return studentImport()
      break;
    case "studentDel"://学生删除
      return studentDel()
      break;
    default:
      break;
  }
}

async function studentDel() {
  for (let index = 0; index < data.length; index++) {
    const element = data[index];
    await cloud.nw.db.del({
      dbName: "admin-class-student-ToC",
      whereJson: { _id: element }
    });
  }
  return cloud.utils.requestReturn({ data: "" })
}

async function studentImport() {
  await cloud.nw.db.del({ dbName: "admin-class-student-ToC", whereJson: { classId: data[0].classId } });
  for (let index = 0; index < data.length; index++) {
    const element = data[index];
    element.userPinyin = pinyin(element.name, { toneType: "none" })
    await cloud.nw.db.add({
      dbName: "admin-class-student-ToC",
      dataJson: element,
      cancelAddTime: false
    });
  }
  return cloud.utils.requestReturn({ data: "" })
}

async function studentsList() {
  let aa = await db.collection("admin-class-student-ToC").find({ classId: data.classId }).toArray()
  return cloud.utils.requestReturn({ data: aa })
}

async function details() {
  let res = await cloud.nw.db.findByWhereJson({
    dbName,
    whereJson: { _id: data._id },
  });
  return cloud.utils.requestReturn({ data: res })
}

async function list() {
  let res = await cloud.nw.db.selects({
    dbName,
    getCount: true,
    pageIndex: data.pageIndex,
    pageSize: data.pageSize,
    // 主表 where 条件
    whereJson: {},
    // 主表排序规则
    sortArr: [{ "name": "_id", "type": "desc" }],
    // 副表列表
    foreignDB: []
  });
  return cloud.utils.requestReturn({ data: res })
}

async function add() {
  let classId = await cloud.nw.db.add({
    dbName,
    dataJson: data,
    cancelAddTime: false
  });
  let obj = {
    classId,
    unitClassHours: data.unitClassHours,
    classStartTime: data.classStartTime,
    classEndTime: data.classEndTime,
    classAddress: data.classAddress,
    teachingTeacher: data.teachingTeacher,
    teachingContent: "",
    classTeacher: data.classTeacher,
  };
  let { classDates } = data;
  if (classDates && classDates.length) {
    for (let index = 0; index < classDates.length; index++) {
      const element = classDates[index];
      let query = { ...obj };
      query["date"] = element;
      query["dateStamp"] = cloud.utils.getTimeFn(element); //开课时间的时间戳
      await cloud.nw.db.add({
        dbName: "admin-teaching-schedule-ToC",
        dataJson: query,
        cancelAddTime: false
      });
    }
  }
  return cloud.utils.requestReturn({ data: "" })
}

async function del() {
  await cloud.nw.db.del({
    dbName,
    whereJson: { _id: data._id }
  });
  //删除对应的排课日期
  await cloud.nw.db.del({
    dbName: "admin-teaching-schedule-ToC",
    whereJson: { classId: data._id }
  });
  //删除对应的学员
  await cloud.nw.db.del({
    dbName: "admin-class-student-ToC",
    whereJson: { classId: data._id }
  });
  return cloud.utils.requestReturn({ data: "" })
}
